<template>
  <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
    <!-- default-checked-keys:默认设置(特性:如果它的值为空数组,你改变值了它会立马渲染,但是如果不是空数组你再修改,它不渲染了) 
    el-tree设置
       node-key="当前数据的唯一标识,一般也是对应选中该项数据后的值"
       default-checked-keys:默认设置哪个项选中
       el-tree   ref=tree
       可以通过this.$refs.tree.setCheckedKeys([需要选中项的id值])
       可以通过this.$refs.tree.getCheckedKeys()获取当前的已选择项
       check-strictly:是否父子选择关联,默认false    
    
    -->
    <el-tree
      ref="tree"
      node-key="id"
      :default-checked-keys="permIds"
      :data="treeData"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
    ></el-tree>

    <template>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index.js'
import { sysRoleGet, sysRolePut, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeData: [],
      permIds: [],
      info: {},
      id: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    closeEvent () {
      this.$emit('update:isShow', false)
      // this.permIds = []
      // 清空默认勾选
      this.$refs.tree.setCheckedKeys([])
    },
    async getData () {
      const res = await sysPermission()
      this.treeData = changeTreeData(res.data, '0')
    },
    // 获取角色详情
    async getRoleInfo (id) {
      this.id = id
      const res = await sysRoleGet(id)
      // this.permIds = res.data.permIds
      this.info = res.data
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情', res)
    },
    // 确定点击
    async submit () {
      // await sysRolePut({
      //   ...this.info,
      //   permIds: this.$refs.tree.getCheckedKeys()
      // })
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('更新权限成功')
      this.closeEvent()
      console.log(this.$refs.tree.getCheckedKeys())
    }
  }
}
</script>
<style></style>
